home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / bind-contrib.tar.gz / bind-contrib.tar / contrib / msql / bind_admin next >
Text File  |  1996-10-25  |  10KB  |  323 lines

  1. #!/usr/local/bin/perl
  2. #
  3. # RCS Id:    $Id: bind_admin,v 8.1 1996/10/25 04:57:41 vixie Exp $
  4. #
  5. # Name:        bind_admin
  6. # Purpose:    Perform administrative tasks on msql_dns db
  7. # Author:    Christopher L Seawood
  8. # Date:        16Jun95
  9. #
  10. # Usage:    bind_admin databasename [-hmsqlhost]
  11. # Description:    Add and remove rhit_dns entries, if a machine entry exists
  12. #
  13. # Tables Used:    msql_dns (read/write) & machines (read)
  14. # References:    model
  15. #
  16. # Change History:
  17. #
  18. # Date       Who    Changes Made
  19. # -------  ---    ------------------------------------------------------------
  20. # 16Jun95  cls    Original Version.
  21. # 21Jul95  cls    Redesigned for cmdline args. 
  22. # 26Jul95  cls  Added simplified db replication. (ie. msqldump...)
  23. {
  24. use Msql;
  25.  
  26. $host = "";
  27. $MsqlDir = "/usr/local/Minerva";
  28.  
  29. if ( @ARGV < 1 ) {
  30.     print "usage: bind_admin databasename [-hhost]\n";
  31.     exit(1);
  32. };
  33.  
  34. $dbname = shift;
  35.  
  36. while (@ARGV) {
  37.     $ack = shift;
  38.     if ( $ack =~ m/^-h\S+/ ) {
  39.     ($host = $ack ) =~ s/^-h//;
  40.     };
  41. };
  42.  
  43. ($dbh = Connect Msql $host, $dbname)
  44.     or die("Can't connect: $Msql::db_errstr\n");
  45.  
  46. do
  47. {
  48.     print <<EOD;
  49.  
  50. 1. List msql_dns entries
  51. 2. Add msql_dns entry
  52. 3. Delete msql_dns entry
  53. 4. Edit msql_dns entry
  54. 5. EXIT
  55. 0. Update other msql servers
  56.  
  57. EOD
  58.     do
  59.     {
  60.     print "Choice? (1-5) ";
  61.     chop ($choice = <STDIN>);
  62.     }
  63.     while ($choice !~ m/^[0-5]$/);
  64.  
  65.     if      ($choice == 1) { &list_msql_dns; }
  66.     elsif ($choice == 2) { &add_msql_dns; }
  67.     elsif ($choice == 3) { &delete_msql_dns; }
  68.     elsif ($choice == 4) { &edit_msql_dns; }
  69.     elsif ($choice == 0) { &update_msql_db };
  70. }
  71. until ($choice == 5);
  72.  
  73. Msql::DESTROY $dbh;
  74.     
  75. }
  76. sub list_msql_dns {
  77.     local ($luck, $stmt,@row);
  78.     print "Enter search condition ('where' clause): ";
  79.     chop ($luck = <STDIN>);
  80.     $luck = "where $luck" if ($luck ne "");
  81.     $luck =~ tr/A-Z/a-z/;
  82.     $stmt = "select * from msql_dns $luck order by dns_entry";
  83.     ($luck = Query $dbh $stmt) or warn "Error $Msql::db_errstr\n$stmt\n";
  84.     print "\n";
  85.     if ($luck) {
  86.     $luck->DataSeek(0);
  87.     while (@row = $luck->FetchRow()){
  88.         printf("%5d, %2d, %40s, %10s, %10s, %127s, %d, %d, %d\n",$row[0],$row[1],$row[2],$row[3],$row[4],$row[5],$row[6],$row[7],$row[8]);
  89.     };
  90.     };
  91.     return;
  92. };
  93.  
  94. sub add_msql_dns {
  95.     local ($last,  $stmt, $name, $zoneid, $domain, $class, $type, $asstime, $import, $dynamic, @row, $id, $info);
  96.  
  97.     $stmt = "select dns_entry from control where one_row = 'A'";
  98.     ($last = Query $dbh $stmt) or warn "Error $Msql::db_errstr\n$stmt\n";
  99.     $id = $row[0] if (@row=$last->FetchRow());
  100.     print "\nEnter the name of the machine to be added: ";
  101.     chop($name = <STDIN>);
  102.     while ($name ne "") {
  103.     print "Enter domain name (e.g. nextwork.rose-hulman.edu.): ";
  104.     chop($domain = <STDIN>);
  105.     $stmt = "select zoneid from msql_zones where name = '$domain'";
  106.     ($last = Query $dbh $stmt) or warn "Error $Msql::db_errstr\n$stmt\n"; 
  107.     $zoneid = $row[0] if (@row=$last->FetchRow());
  108.     print "Enter class: ";
  109.     chop($class = <STDIN>);
  110.     $class =~ tr/a-z/A-Z/;
  111.     print "Enter type: ";
  112.     chop($type = <STDIN>);
  113.     $type =~ tr/a-z/A-Z/;
  114.     print "Enter info: ";
  115.     chop($info = <STDIN>);
  116.     print "Enter importance: ";
  117.     chop($import  = <STDIN>);
  118.     print "Dynamic? (y/n) ";
  119.     chop($ack=<STDIN>);
  120.     $dynamic = 0;
  121.     $dynamic = 1 if ($ack =~ /[yY]/);
  122.     $asstime = time();
  123.     
  124.     printf("\n\nMachine:       %s\n",$name);
  125.     printf("Domain:           %s\n",$domain);
  126.     printf("Class:           %s\n",$class);
  127.     printf("Type:           %s\n",$type);
  128.     printf("Info:           %s\n",$info);
  129.     printf("Assigned time: %d\n",$asstime);
  130.     printf("Importance:    %d\n",$import);
  131.     printf("Dynamic:       %d\n",$dynamic);
  132.     print "\n\nDo you accept these values? (Y/N) ";
  133.     chop($ack = <STDIN>);
  134.     if ($ack =~ /[Yy]/){
  135.         $id++;
  136.         $stmt = "insert into msql_dns (dns_entry, zoneid, machine, class, type, info, assigned_time, importance, dynamic) values ($id, $zoneid, '$name', '$class','$type','$info',$asstime,$import,$dynamic)";
  137.         Query $dbh $stmt or warn "Error $Msql::db_errstr\n$stmt\n";
  138.         print "MSQL_DNS entry $name.$domain added to $dbname.\n";
  139.     };
  140.     print "\nEnter the name of the machine to be added: ";
  141.     chop($name = <STDIN>);
  142.     };
  143.     $stmt = "update control set dns_entry = $id where one_row = 'A'";
  144.     Query $dbh $stmt or warn "Error $Msql::db_errstr\n$stmt\n";
  145.     return;
  146. }
  147.  
  148. sub delete_msql_dns {
  149.     local ($last, @row, $stmt, $name, $ack, $entry );
  150.     do {
  151.     print "Enter the dns_entry of the machine to delete: ";
  152.     chop($entry=<STDIN>);
  153.     return if ($entry eq "");
  154.     $stmt = "select * from msql_dns where dns_entry = $entry";
  155.     ($last = Query $dbh $stmt) or warn "Error $Msql::db_errstr\n$stmt\n";
  156.     if (@row=$last->FetchRow()){
  157.         printf("\n\nMachine:       %s\n",$row[2]);
  158.         printf("Zoneid:       %s\n",$row[1]);
  159.         printf("Class:       %s\n",$row[3]);
  160.         printf("Type:       %s\n",$row[4]);
  161.         printf("Info:       %s\n",$row[5]);
  162.         printf("Assigned time: %d\n",$row[6]);
  163.         printf("Importance:       %d\n",$row[7]);
  164.         printf("Dynamic:       %d\n",$row[8]);
  165.         print "\n\nDo you really want to remove this msql_dns entry? (Y/N) ";
  166.         chop($ack = <STDIN>);
  167.         if ($ack =~ /[Yy]/){
  168.         $stmt = "delete from msql_dns where dns_entry = $entry";
  169.         Query $dbh $stmt or warn "Error $Msql::db_errstr\n$stmt\n";
  170.         print "MSQL_DNS entry $entry with machine $row[2] removed from $dbname.\n";
  171.         };
  172.     } else {
  173.         print "Invalid dns_entry. Nothing removed.\n";
  174.     }
  175.     } while ($entry ne ""); 
  176.     return;
  177. }
  178.  
  179. sub edit_msql_dns {
  180.     local ($last,  @row, $stmt, $entry, $name, $domain, $zoneid, $class, $type, $asstime, $ack, $num, $done, $import, $dynamic, $info);
  181.     $num = "";
  182.     do {
  183.     print "\nEnter the dns_entry to edit: ";
  184.     chop($entry=<STDIN>);
  185.     return if ($entry eq "");
  186.     $stmt = "select * from msql_dns where dns_entry = $entry";
  187.     ($last = Query $dbh $stmt) or warn "Error $Msql::db_errstr\n$stmt\n";
  188.     if (@row=$last->FetchRow()){
  189.         $done = 'NO';
  190.         $zoneid = $row[1];
  191.         $name = $row[2];
  192.         $class = $row[3];
  193.         $type = $row[4];
  194.         $info = $row[5];
  195.         $asstime = $row[6];
  196.         $import = $row[7];
  197.         $dynamic = $row[8];
  198.         $stmt = "select name from msql_zones where zoneid = $zoneid";
  199.         ($last = Query $dbh $stmt) or warn "Error $Msql::db_errstr\n$stmt\n";        
  200.         $domain = $row[0] if (@row=$last->FetchRow());
  201.         do {
  202.         do {
  203.             printf("\n\n1. Change hostname:   %s\n",$name);
  204.             printf("2. Change domain:      %s\n",$domain); 
  205.             printf("3. Change class:      %s\n",$class); 
  206.             printf("4. Change type:      %s\n",$type);
  207.             printf("5. Change info:      %s\n",$info);
  208.             printf("6. Change importance: %d\n",$import);
  209.             printf("7. Dynamic?          %d\n",$dynamic);
  210.             printf("   Assigned time:      %d\n",$asstime);
  211.             print "9. Accept changes\n";
  212.             print "0. Reject changes\n";
  213.             print "\nChoice? (1-7,9-0) ";
  214.             chop($num = <STDIN>);
  215.         } while ($num !~ /[123456790]/);
  216.         
  217.         if ($num eq "1"){
  218.             do {
  219.             print "\nEnter new hostname: ";
  220.             chop($name = <STDIN>);
  221.             } while ($name eq "");
  222.         } elsif ($num eq "2"){
  223.             $olddom = $domain;
  224.             do {        
  225.             print "\nEnter new domain: ";
  226.             chop($domain = <STDIN>);
  227.             } while ($domain eq "");
  228.             $stmt = "select zoneid from msql_zones where name = '$domain'";
  229.             ($last = Query $dbh $stmt) or warn "Error $Msql::db_errstr\n$stmt\n"; 
  230.             if (@row=$last->FetchRow()){
  231.             $zoneid = $row[0];
  232.             } else {
  233.             print "Domain $domain not found. Using domain $olddom .\n";
  234.             $domain = $olddom;
  235.             };     
  236.         } elsif ($num eq "3"){
  237.             do {
  238.             print "\nEnter new class: ";
  239.             chop($class=<STDIN>);
  240.             } while ($class eq "");
  241.             $class =~ tr/a-z/A-Z/;
  242.         } elsif ($num eq "4"){
  243.             do {
  244.             print "\nEnter new type: ";
  245.             chop($type=<STDIN>);
  246.             } while ($type eq "");
  247.             $type =~ tr/a-z/A-Z/;
  248.         } elsif ($num eq "5"){
  249.             do {
  250.             print "\nEnter new info: ";
  251.             chop($info=<STDIN>);
  252.         } while ($info eq "");
  253.         } elsif ($num eq "6"){
  254.         do {
  255.             print "\nEnter new info: ";
  256.             chop($info=<STDIN>);
  257.         } while ($info eq "");
  258.         } elsif ($num eq "7"){
  259.         do {
  260.             print "\nDynamic ";
  261.             chop($ack=<STDIN>);
  262.         } while ($ack !~ /[yYnN]/);
  263.         if ($ack =~ /[yY]/ ){
  264.            $dynamic = 1;
  265.             } else { $dynamic = 0 };
  266.         } elsif ($num eq "9"){
  267.         $done = 'YES';
  268.         } elsif ($num eq "0"){
  269.         return;
  270.         } else {
  271.         return;        # Should never be here
  272.         }
  273.     } while ($done ne 'YES');
  274.         
  275.         $asstime = time();
  276.         $stmt = "update msql_dns set machine='$name', zoneid=$zoneid, class='$class',type='$type',info = '$info', assigned_time = $asstime, importance=$import, dynamic=$dynamic where dns_entry = $entry";
  277.     Query $dbh $stmt or warn "Error $Msql::db_errstr\n$stmt\n";
  278.     print "MSQL_DNS entry $entry updated in $dbname.\n";
  279.     } else {
  280.         print "\nNo such dns_entry. Nothing updated.\n";
  281.     };
  282.     } while ($entry ne "");
  283.     return;
  284. };
  285.  
  286. sub update_msql_db{
  287.     local ($stmt, $name, @row);
  288.     print "Enter the name of the server to update: ";
  289.     chop($name=<STDIN>);
  290.     while ($name ne ""){
  291.     if ($host ne ""){
  292.         open (ACK,"$MsqlDir/bin/msqldump -h $host $dbname msql_dns |");
  293.     } else {
  294.         open (ACK,"$MsqlDir/bin/msqldump $dbname msql_dns |");
  295.     };
  296.  
  297.     open (DOH, "|$MsqlDir/bin/msql -h $name $dbname >/dev/null");
  298.     print DOH "DROP TABLE msql_dns\\g\n";    
  299.     while (<ACK>){
  300.         print DOH <ACK>;
  301.     };
  302.     close(<ACK>);
  303.  
  304.     if ($host ne ""){
  305.         open (JOY,"$MsqlDir/bin/msqldump -h $host $dbname msql_zones |");
  306.     } else {
  307.         open (JOY,"$MsqlDir/bin/msqldump $dbname msql_zones |");
  308.     };
  309.  
  310.     print DOH "DROP TABLE msql_zones\\g\n";    
  311.     while (<JOY>){
  312.         print DOH <JOY>;
  313.     };
  314.     close(<JOY>);
  315.     $stmt = "select zoneid, dns_entry from control where one_row = 'A'";
  316.     ($last = Query $dbh $stmt) or warn "Error $Msql::db_errstr\n$stmt\n";
  317.     print DOH "update control set zoneid = $row[0], dns_entry = $row[1] where one_row = 'A'\\g" if (@row=$last->FetchRow());
  318.     print DOH "\\q\n";
  319.     print "\n\nEnter the name of the server to update: ";
  320.     chop($name=<STDIN>);
  321.     };
  322. }
  323.